package de.cube3d.blitwit.test 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	public class Util 
	{
		
		// primitive random number generator
		private static var m_z:Number = 12034;
		private static var m_w:Number = 5943;
		/**
		 * Sets the initial seed values for the random generator. Passing 
		 * nothing will set it back to default.
		 * 
		 * @param	r1
		 * @param	r2
		 * @param	r3
		 */
		public static function rseed(r1:Number=12034, r2:Number=5943):void {
			m_z = r1;
			m_w = r2;
		}
		/**
		 * from http://en.wikipedia.org/wiki/Random_number_generation
		 * 
		 * We need some deterministic random numbers to make reproducable tests that 
		 * include randomness. Thus, this code should do.
		 * 
		 * @return 
		 */
		public static function randomInt():uint {
			m_z = 36969 * (m_z & 65535) + (m_z >> 16);
			m_w = 18000 * (m_w & 65535) + (m_w >> 16);
			return (m_z << 16) + m_w;
		}
		
		public static function random():Number {
			// 0 <= u < 2^32
			var u:uint = randomInt();
			// The magic number below is 1/(2^32 + 2).
			// The result is strictly between 0 and 1.
			return (u + 1.0) * 2.328306435454494e-10;
		}
		
		// bitmap asset access
		private static var _bmcache:Object = { };
		/**
		 * Returns a BitmapData object for the given name. Note that it returns the original
		 * bitmap and that it is the same for each call with the same name.
		 * 
		 * @param	name Name of the asset, see "Resource" class for names.
		 * @return
		 */
		public static function getBitmap(name:String):BitmapData {
			if (_bmcache[name]) return _bmcache[name] as BitmapData;
			var c:Class = Resource["x_"+name];
			return _bmcache[name] = (new c() as Bitmap).bitmapData;
		}
	}

}